<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>树形结构中找到子节点的父级路径</title>
  <!--  https://blog.csdn.net/kukudelaomao/article/details/88599307 -->
</head>

<body>

</body>
<script>
  "use strict"; {
    let data = [
      {
        icon: "0_icon",
        menu_id: "0",
        name: "0",
        children: [
          {
            icon: "00_icon",
            menu_id: "0-0",
            name: "00",
            parent_id: "0",
            children: []
          },
          {
            icon: "01_icon",
            menu_id: "0-1",
            name: "01",
            parent_id: "0",
            children: []
          }
        ]
      },
      {
        icon: "1_icon",
        menu_id: "1",
        name: "1",
        children: [
          {
            icon: "10_icon",
            menu_id: "1-0",
            name: "10",
            parent_id: "1",
            children: [
              {
                icon: "100_icon",
                menu_id: "1-0-0",
                name: "100",
                parent_id: "1-0",
                children: []
              }
            ]
          },
          {
            icon: "11_icon",
            menu_id: "1-1",
            name: "11",
            parent_id: "1",
            children: []
          }
        ]
      },
      {
        icon: "2_icon",
        menu_id: "2",
        name: "2",
        children: [
          {
            icon: "20_icon",
            menu_id: "2-0",
            name: "20",
            parent_id: "2",
            children: [
              {
                icon: "200_icon",
                menu_id: "2-0-0",
                name: "200",
                parent_id: "2-0",
                children: []
              }
            ]
          },
          {
            icon: "21_icon",
            menu_id: "2-1",
            name: "21",
            parent_id: "2",
            children: []
          },
          {
            icon: "22_icon",
            menu_id: "2-2",
            name: "22",
            parent_id: "2",
            children: []
          }
        ]
      },
      {
        icon: "3_icon",
        menu_id: "3",
        name: "3",
        children: []
      }
    ];

    let data_name = "menu_id";
    // let data_value = "3";
    // let data_value = "2";
    let data_value = "2-1";
    // let data_value = "2-0-0";
    let get_data = (data, data_name, data_value, index_arr) => {
      let arr = Array.from(index_arr);
      for (let value of data.values()) {
        arr.push({
          icon: value.icon,
          menu_id: value.menu_id,
          name: value.name
        });
        if (value[data_name] == data_value) return arr;
        if (value["children"] && value["children"].length) {
          let res = get_data(value["children"], data_name, data_value, arr);
          if (res) return res;
        };
        arr.pop();
      };
      return false;
    };
    let i = get_data(data, data_name, data_value, []);
    console.log(JSON.stringify(i, null, 2));
  };
</script>

</html>